var express = require("express");
var router = express.Router();
var mysql = require("./tool/mysql");
var url = require("url");
const fs = require("fs");
const { resolve } = require("path");

/* GET home page. */
var isLogin = 0;
router.get("/", function(req, res, next) {
  console.log(req.cookies.isLogin)
  // if(req.cookies.isLogin == 0) {
  //   // 登录
  //   res.render ("login",{title:"精彩家庭-登录",errInfo:""});
  // }else 
  if (req.cookies.isLogin == 1){
    res.render("index", { title: "首页", activeIndex: 0, isBoss: false });
  }else {
    res.render ("login",{title:"精彩家庭-登录",errInfo:""});
  }
});
// 精彩家庭导航
router.get("/nav", function(req, res, next) {
  mysql.connect(function(db) {
    var queryObj = {};
    var showObj = {};
    mysql.find(db, "nav", queryObj, showObj, function(result) {
      res.render("nav", {
        title: "精彩家庭-nav导航",
        activeIndex: 5,
        isBoss: false,
        result: result
      });
    });
  });
});

// 添加精彩家庭导航信息
router.get("/addnav", function(req, res, next) {
  res.render("addnav", {
    title: "精彩家庭-添加nav",
    activeIndex: 5,
    isBoss: false,
    tip: ""
  });
});
// 精彩家庭添加信息页面中的表单提交
router.post("/addnavAction", function(req, res, next) {
  mysql.connect(function(db) {
    var insertData = req.body;
    var queryObj = { navID: insertData.navID };
    var showObj = {};
    mysql.find(db, "nav", queryObj, showObj, function(result) {
      console.log(result);
      if (result.length == 0) {
        mysql.insert(db, "nav", insertData, function(insertResult) {
          // res.render('nav', {title:'精彩家庭-nav导航', activeIndex: 5, isBoss: false})
          // 重定向
          res.redirect("/nav");
        });
      } else {
        res.render("addnav", {
          title: "精彩家庭-添加nav",
          activeIndex: 5,
          isBoss: false,
          tip: "该nav项已经存在、已添加"
        });
      }
      db.close();
      // res.render('addnav', {title:'精彩家庭-添加nav', activeIndex: 5, isBoss: false})
    });
  });
});

// 更新编辑事件信息
router.get("/updateNav", function(req, res, next) {
  var id = url.parse(req.url, true).query.id;
  // console.log(id)
  mysql.connect(function(db) {
    // console.log("success-nav");
    var queryObj = { navID: id };
    var showObj = {
      _id: 0
    };
    mysql.find(db, "nav", queryObj, showObj, function(result) {
      obj = {
        title: "精彩家庭-nav编辑",
        activeIndex: 5,
        isBoss: false,
        result: result
      };
      // console.log(obj)
      res.render("updateNav", obj);
      db.close();
    });
  });
});
//精彩家庭更新数据form表单
router.post("/updateNavAction", function(req, res, next) {
  console.log(req.body);
  var updateObj = req.body;
  var whereObj = {
    navID: updateObj.navID
  };
  mysql.connect(function(db) {
    mysql.updateOne(db, "nav", whereObj, updateObj, function(result) {
      res.send("<script>window.location.href='/nav'</script>");
      db.close();
    });
  });
});

// 删除事件页面
// 进入删除页面
router.get("/deleteNav", function(req, res, next) {
  var id = url.parse(req.url, true).query.id;
  obj = {
    title: "精彩家庭-删除信息",
    activeIndex: 1,
    isBoss: false,
    navID: id
  };
  mysql.connect(function(db) {
    var deleteObj = {
      navID: id
    };
    mysql.deleteOne(db, "nav", deleteObj, function(result) {
      res.send("<script>window.location.href='/nav'</script>");
      db.close();
    });
  });
});

// 平台
// 平台页面
router.get("/platform", function(req, res, next) {
  var platformType = url.parse(req.url, true).query.platformType * 1;
  var platStr = "";
  switch (platformType) {
    case 0:
      platStr = "平台介绍";
      break;
    case 1:
      platStr = "平台架构";
      break;
    case 2:
      platStr = "平台文化";
  }
  mysql.connect(function(db) {
    console.log("success-------platform");
    var queryObj = {
      platformID: 34 + platformType
    };
    var showObj = {
      _id: 0,
      platformID: 1,
      platformType: 1,
      content: 1
    };
    mysql.find(db, "platform", queryObj, showObj, function(result) {
      // 获取json数据，写入文件，好看
      // fs.writeFileSync(resolve('./log.json'), JSON.stringify(result, null, 2), 'utf8');
      res.render("platform", {
        title: "精彩家庭平台管理",
        activeIndex: 7,
        isBoss: true,
        platStr: platStr,
        platformType: platformType,
        result: result
      });
      db.close();
    });
  });
});

// 添加并编辑平台附文本页面
router.get("/platform/addplat", function(req, res, next) {
  var platformType = url.parse(req.url, true).query.platformType;
  mysql.connect(function(db) {
    console.log("success-platform");
    var querObj = { platformType: platformType };
    var showObj = {
      _id: 0,
      platformType: 1,
      content: 1
    };
    mysql.find(db, "platform", querObj, showObj, function(result) {
      console.log(result);
      var platStr = "";
      switch (platformType) {
        case 0:
          platStr = "平台简介";
          break;
        case 1:
          platStr = "平台架构";
          break;
        case 2:
          platStr = "平台文化";
      }
      res.render("addplat", {
        title: "精彩家庭-平台管理",
        activeIndex: 7,
        isBoss: true,
        platStr: platStr,
        platformType: platformType,
        result: result
      });
      db.close();
    });
  });
});


// 平台表单提交
router.post("/platform/addplatformAction", function(req, res, next) {
  mysql.connect(function(db) {
    var platformType = req.body.platformType;
    var findData = {
      platformType: platformType
    };
    var showObj = {};
    mysql.find(db, "platform", findData, showObj, function(result) {
      var data = result[0];
      data.content = req.body.editor1;
      mysql.updateOne(db, "platform", findData, data, function(result) {
        //重定向
        res.redirect("/platform?platformType=" + platformType);
        db.close();
      });
    });
  });
});

//  删除content
router.get('/platform/deleteplatform',function(req, res, next){
  mysql.connect(function(db) {
    var platformType = url.parse(req.url,true).query.platformType;
    var findData = {
      platformType: platformType
    };
    var showObj = {};
    mysql.find(db, "platform", findData, showObj, function(result) {
      var data = result[0];
      data.content = '';
      mysql.updateOne(db, "platform", findData, data, function(result) {
        //重定向
        res.redirect("/platform?platformType=" + platformType);
        db.close();
      });
    });
  });
});

module.exports = router;
